import { ElNotification } from "element-plus";
import router from ".";
import { useStore } from "../stores";

// 路由前置守卫
router.beforeEach((to, from, next) => {
  const store = useStore()
  const token = store.userToken

  // 若登录则返回主页面
  if (token && to.name === 'login') next({name: 'home'})
  // 若未登录 则无法更改用户信息/写博客/聊天 且进入登录页面
  if (!token && (to.matched[0].path === '/profile' || to.name === 'editblog' || to.name === 'chat')) {
    next({name: 'login'})
    ElNotification({
      title: '您还未登录',
      message: '请先登录哦',
      type: 'warning'
    })
  }
  // 判断是否为管理员
  if (to.name === 'admin' && !store.admin) {
    next({name: 'home'})
    ElNotification({
      title: 'Stop!',
      message: '没有权限哦',
      type: 'error'
    })
  }

  next()
})

// 路由后置守卫
// router.afterEach(() => {})

export default router